summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/time/time_zone_service.cpp
blob: 6d05a66f1d6255fb19b5ae35c3dedb1529522cf2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/logging/log.h"
#include "core/hle/ipc_helpers.h"
#include "core/hle/service/time/time_zone_content_manager.h"
#include "core/hle/service/time/time_zone_service.h"
#include "core/hle/service/time/time_zone_types.h"

namespace Service::Time {

ITimeZoneService::ITimeZoneService(Core::System& system_,
                                   TimeZone::TimeZoneContentManager& time_zone_manager_)
    : ServiceFramework{system_, "ITimeZoneService"}, time_zone_content_manager{time_zone_manager_} {
    static const FunctionInfo functions[] = {
        {0, &ITimeZoneService::GetDeviceLocationName, "GetDeviceLocationName"},
        {1, nullptr, "SetDeviceLocationName"},
        {2, nullptr, "GetTotalLocationNameCount"},
        {3, nullptr, "LoadLocationNameList"},
        {4, &ITimeZoneService::LoadTimeZoneRule, "LoadTimeZoneRule"},
        {5, nullptr, "GetTimeZoneRuleVersion"},
        {6, nullptr, "GetDeviceLocationNameAndUpdatedTime"},
        {100, &ITimeZoneService::ToCalendarTime, "ToCalendarTime"},
        {101, &ITimeZoneService::ToCalendarTimeWithMyRule, "ToCalendarTimeWithMyRule"},
        {201, &ITimeZoneService::ToPosixTime, "ToPosixTime"},
        {202, &ITimeZoneService::ToPosixTimeWithMyRule, "ToPosixTimeWithMyRule"},
    };
    RegisterHandlers(functions);
}

void ITimeZoneService::GetDeviceLocationName(Kernel::HLERequestContext& ctx) {
    LOG_DEBUG(Service_Time, "called");

    TimeZone::LocationName location_name{};
    if (const Result result{
            time_zone_content_manager.GetTimeZoneManager().GetDeviceLocationName(location_name)};
        result != ResultSuccess) {
        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
        return;
    }

    IPC::ResponseBuilder rb{ctx, (sizeof(location_name) / 4) + 2};
    rb.Push(ResultSuccess);
    rb.PushRaw(location_name);
}

void ITimeZoneService::LoadTimeZoneRule(Kernel::HLERequestContext& ctx) {
    IPC::RequestParser rp{ctx};
    const auto raw_location_name{rp.PopRaw<std::array<u8, 0x24>>()};

    std::string location_name;
    for (const auto& byte : raw_location_name) {
        // Strip extra bytes
        if (byte == '\0') {
            break;
        }
        location_name.push_back(byte);
    }

    LOG_DEBUG(Service_Time, "called, location_name={}", location_name);

    TimeZone::TimeZoneRule time_zone_rule{};
    if (const Result result{
            time_zone_content_manager.LoadTimeZoneRule(time_zone_rule, location_name)};
        result != ResultSuccess) {
        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
        return;
    }

    std::vector<u8> time_zone_rule_outbuffer(sizeof(TimeZone::TimeZoneRule));
    std::memcpy(time_zone_rule_outbuffer.data(), &time_zone_rule, sizeof(TimeZone::TimeZoneRule));
    ctx.WriteBuffer(time_zone_rule_outbuffer);

    IPC::ResponseBuilder rb{ctx, 2};
    rb.Push(ResultSuccess);
}

void ITimeZoneService::ToCalendarTime(Kernel::HLERequestContext& ctx) {
    IPC::RequestParser rp{ctx};
    const auto posix_time{rp.Pop<s64>()};

    LOG_DEBUG(Service_Time, "called, posix_time=0x{:016X}", posix_time);

    TimeZone::TimeZoneRule time_zone_rule{};
    const auto buffer{ctx.ReadBufferSpan()};
    std::memcpy(&time_zone_rule, buffer.data(), buffer.size());

    TimeZone::CalendarInfo calendar_info{};
    if (const Result result{time_zone_content_manager.GetTimeZoneManager().ToCalendarTime(
            time_zone_rule, posix_time, calendar_info)};
        result != ResultSuccess) {
        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
        return;
    }

    IPC::ResponseBuilder rb{ctx, 2 + (sizeof(TimeZone::CalendarInfo) / 4)};
    rb.Push(ResultSuccess);
    rb.PushRaw(calendar_info);
}

void ITimeZoneService::ToCalendarTimeWithMyRule(Kernel::HLERequestContext& ctx) {
    IPC::RequestParser rp{ctx};
    const auto posix_time{rp.Pop<s64>()};

    LOG_DEBUG(Service_Time, "called, posix_time=0x{:016X}", posix_time);

    TimeZone::CalendarInfo calendar_info{};
    if (const Result result{
            time_zone_content_manager.GetTimeZoneManager().ToCalendarTimeWithMyRules(
                posix_time, calendar_info)};
        result != ResultSuccess) {
        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
        return;
    }

    IPC::ResponseBuilder rb{ctx, 2 + (sizeof(TimeZone::CalendarInfo) / 4)};
    rb.Push(ResultSuccess);
    rb.PushRaw(calendar_info);
}

void ITimeZoneService::ToPosixTime(Kernel::HLERequestContext& ctx) {
    LOG_DEBUG(Service_Time, "called");

    IPC::RequestParser rp{ctx};
    const auto calendar_time{rp.PopRaw<TimeZone::CalendarTime>()};
    TimeZone::TimeZoneRule time_zone_rule{};
    std::memcpy(&time_zone_rule, ctx.ReadBufferSpan().data(), sizeof(TimeZone::TimeZoneRule));

    s64 posix_time{};
    if (const Result result{time_zone_content_manager.GetTimeZoneManager().ToPosixTime(
            time_zone_rule, calendar_time, posix_time)};
        result != ResultSuccess) {
        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
        return;
    }

    ctx.WriteBuffer(posix_time);

    // TODO(bunnei): Handle multiple times
    IPC::ResponseBuilder rb{ctx, 3};
    rb.Push(ResultSuccess);
    rb.PushRaw<u32>(1); // Number of times we're returning
}

void ITimeZoneService::ToPosixTimeWithMyRule(Kernel::HLERequestContext& ctx) {
    LOG_DEBUG(Service_Time, "called");

    IPC::RequestParser rp{ctx};
    const auto calendar_time{rp.PopRaw<TimeZone::CalendarTime>()};

    s64 posix_time{};
    if (const Result result{time_zone_content_manager.GetTimeZoneManager().ToPosixTimeWithMyRule(
            calendar_time, posix_time)};
        result != ResultSuccess) {
        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
        return;
    }

    ctx.WriteBuffer(posix_time);

    IPC::ResponseBuilder rb{ctx, 3};
    rb.Push(ResultSuccess);
    rb.PushRaw<u32>(1); // Number of times we're returning
}

} // namespace Service::Time